#region Using Statements
using System;
using System.Xml;
using System.Collections.Generic;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using MegaEngine.Source.Sprites;
using MegaEngine.Source.Levels;
using MegaEngine.Source.Tiles;
using MegaEngine.Source.Font;
#endregion

namespace MegaEngine
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MegaEngineController : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;
        ArrayList sprites = new ArrayList();
        ArrayList bullets = new ArrayList();
        KeyboardState keyboard = Keyboard.GetState();
        MegaEngineGame level;
        SpriteBatch spriteBatch;
        XNAFont fontRenderer;

        public MegaEngineController()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
            Initialize();
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            level = new Metroid();
            //level = new MetroidLevelEditor();
            //level = new Tetris();
            base.Initialize();
        }


        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        ///             


        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                //load the textures for the level
                for (int i = 0; i < level.tilePaths.Length; i++)
                {
                    level.tileTextures[i] = content.Load<Texture2D>(level.tilePaths[i]) as Texture2D;
                }
                for (int i = 0; i < level.spritePaths.Length; i++)
                {
                    level.spriteTextures[i] = content.Load<Texture2D>(level.spritePaths[i]) as Texture2D;
                }
                level.init();
                spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
                fontRenderer = XNAFont.LoadFont(graphics.GraphicsDevice, "Content\\Textures\\Fonts\\test.dds.xml", content.Load<Texture2D>("Content\\Textures\\Fonts\\test"));
            }
        }


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                content.Unload();
            }
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            ArrayList bulletCleanup = new ArrayList();
            ArrayList enemyCleanup = new ArrayList();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            level.update(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.White);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            ArrayList tiles = level.getViewFrame();
            foreach (Tile t in tiles)
            {
                spriteBatch.Draw(t.theTexture, t.bounds, Color.White);
            }
            ArrayList spriteFrame = level.getSpriteFrame();
            foreach (Sprite s in spriteFrame)
            {
                s.render(spriteBatch);
            }
            if (level.playerAvatar != null)
            {
                level.playerAvatar.render(spriteBatch);
            }
            spriteBatch.End();
            level.renderHud(fontRenderer, true, gameTime);

            base.Draw(gameTime);
        }
    }
}
